package com.cloud.io;

import java.io.*;

/**
 * 参考：
 * https://www.jianshu.com/p/ad637908be8e
 * FileInputStream和FileOutputStream测试
 * 对文件流进行操作
 */
public class FileInputStreamTest {

    /**
     * 复制文件内容
     * @param inFile 源文件
     * @param outFile 目标文件
     */
    public static void readFileByBytes(String inFile, String outFile) {
        InputStream in = null;
        OutputStream out = null;
        try {
            byte[] tempBytes = new byte[1024];
            int byteRead = 0;
            in = new FileInputStream(inFile);
            out = new FileOutputStream(outFile);
            while((byteRead = in.read(tempBytes)) != -1) {
                out.write(tempBytes, 0, byteRead);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        readFileByBytes("src/main/resources/shu.txt", "src/main/resources/shu2.txt");
    }
}
